import os
import oss2
from app.config import Config
from datetime import datetime
from uuid import uuid4
from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.request import CommonRequest
from werkzeug.contrib.cache import FileSystemCache
import tempfile


auth = oss2.Auth(Config.OSS_ACCESS_KEY_ID, Config.OSS_ACCESS_KEY_SECRET)
oss_bucket = oss2.Bucket(auth, Config.OSS_END_POINT, Config.OSS_BUCKET)


def generate_pdf_name():
    directory = datetime.now().strftime('%Y-%m-%d')
    file_name = '{}.pdf'.format(uuid4())
    path = os.path.join(Config.PDF_UPLOAD_PATH, directory)
    if not os.path.exists(path):
        os.makedirs(path)
    return os.path.join(directory, file_name)


def oss_sms(*args, tel, code):
    client = AcsClient('LTAIMIXKOAaTSFiD', 'IjTcImvdMEQCAvRr5Pn8Yhy0VR6u6H')
    request = CommonRequest()
    request.set_accept_format('json')
    request.set_domain('dysmsapi.aliyuncs.com')
    request.set_method('POST')
    request.set_protocol_type('https')
    request.set_version('2017-05-25')
    request.set_action_name('SendSms')

    request.add_query_param('PhoneNumbers', tel)
    request.add_query_param('SignName', "无限丽水")
    request.add_query_param('TemplateCode', "SMS_137875023")
    request.add_query_param('TemplateParam', {'code': code})
    response = client.do_action(request)
    return response


def make_cache():
    return FileSystemCache(tempfile.TemporaryDirectory().name)


cache = make_cache()
